/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.codeminer.parts.files;

import java.io.File;
import java.util.List;
import org.codeminer.database.DatabaseException;
import org.codeminer.database.DatabaseManager;
import org.codeminer.parts.application.ApplicationContext;
import org.codeminer.parts.database.AlbumConnector;
import org.codeminer.parts.database.ArtistConnector;
import org.codeminer.parts.database.SongConnector;
import org.codeminer.parts.library.Album;
import org.codeminer.parts.library.Artist;
import org.codeminer.parts.library.Song;

/**
 *
 * @author Daniel Wasilew <daniel.wasilew@codeminer.org>
 */
public class StoreInDatabaseProcessor implements FileProcessor<ProcessInfo> {

    @Override
    public void process(ProcessInfo param, ApplicationContext applicationContext) throws ProcessException {
        String title = param.getStringValue(ProcessInfo.TrackKeys.TITLE);
        int trackNumber = Integer.parseInt(param.getStringValue(ProcessInfo.TrackKeys.TRACK));
        int year = Integer.parseInt(param.getStringValue(ProcessInfo.TrackKeys.YEAR));

        double duration = param.getIntegerValue(ProcessInfo.TrackKeys.DURATION);
        File file = param.getFile();

        DatabaseManager databaseManager = applicationContext.getApplicationPart(DatabaseManager.class);
        try {
            String artistName = param.getStringValue(ProcessInfo.TrackKeys.ARTIST);
            String albumName = param.getStringValue(ProcessInfo.TrackKeys.ALBUM);
            final ArtistConnector artistConnector = (ArtistConnector) databaseManager.getConnector(ArtistConnector.class);
            final SongConnector songConnector = (SongConnector) databaseManager.getConnector(SongConnector.class);
            final AlbumConnector albumConnector = (AlbumConnector) databaseManager.getConnector(AlbumConnector.class);
            
            Artist artist = artistConnector.getArtist(Artist.getNameCriteria(artistName));
            if (artist == null) {
                artist = artistConnector.storeArtist(new Artist(artistName));
            }

            Album album = null;

            List<Album> albums = albumConnector.getAlbums(Album.byName(albumName));
            if (albums.isEmpty()) {
                album = albumConnector.storeAlbum(new Album(year, albumName, null));
            } else if (albums.size() == 1) {
                album = albums.get(0);
            } else {
                List<Album> albumsForArtist = albumConnector.getAlbums(artist);
                for (Album found : albums) {
                    if (albumsForArtist.contains(found)) {
                        album = found;
                        break;
                    }
                }
            }

            artistConnector.addAlbumToArtist(album, artist);

            Song song = new Song(title, trackNumber, duration, file);

            song = songConnector.storeSong(song);
            albumConnector.addSongToAlbum(song, album);
            artistConnector.addSongToArtist(song, artist);
            artistConnector.addAlbumToArtist(album, artist);

            param.put(ProcessInfo.TrackKeys.SONG, song);
            ExtractArtworksProcessor extractArtworksProcessor = new ExtractArtworksProcessor();
            extractArtworksProcessor.process(param, applicationContext);
        } catch (DatabaseException ex) {
            throw new ProcessException("could not store song", ex);
        }
    }
}
